<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, ref, unref, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { FeatureType } from '@/api/check/feature/types'
import { ElCollapse, ElCollapseItem, ElForm, ElFormItem, ElInput } from 'element-plus'

const { required, lengthRange } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<FeatureType>>,
    default: () => null
  }
  // formSchema: {
  //   type: Array as PropType<FormSchema[]>,
  //   default: () => []
  // }
})

const schema1 = reactive<FormSchema[]>([
  {
    field: 'test_test',
    label: '材料/零件图号',
    colProps: {
      span: 6
    },
    formItemProps: {
      slots: {
        default: (data) => {
          return <ElInput v-model={data.test_test}></ElInput>
        }
      }
    }
  }
])

const schema = reactive<FormSchema[]>([
  {
    field: 'part_id',
    label: '材料/零件图号',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'part_name',
    label: '材料/零件名称',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'material_ids',
    label: '物料编号',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'product_type',
    label: '型号',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'category',
    label: '材料类型',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'material_type',
    label: '物料类型',
    colProps: {
      span: 6
    },
    component: 'Input'
  },

  {
    field: 'is_lift_limit',
    label: '是否限寿件',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'life_limit_requirement',
    label: '限寿要求',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'standard',
    label: '规范/标准',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'standard_version',
    label: '规范/标准版次',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'is_sample',
    label: '抽检',
    colProps: {
      span: 6
    },
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '是',
          value: true
        },
        {
          label: '否',
          value: false
        }
      ]
    }
  },
  {
    field: 'plan_remark',
    label: '计划备注',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'require_size',
    label: '要求尺寸',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'sample_size',
    label: '试样规格/尺寸',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'sample_number',
    label: '试样数量',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: 'remark',
    label: '备注',
    colProps: {
      span: 6
    },
    component: 'Input'
  },
  {
    field: '',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: (data) => {
          return (
            <ElCollapse accordion class="w-full">
              <ElCollapseItem title="零件/材料标记" name="1">
                <ElFormItem label="测试项1">
                  <ElInput v-model={data.test1}></ElInput>
                </ElFormItem>
                <ElFormItem label="测试项2">
                  <ElInput v-model={data.test2}></ElInput>
                </ElFormItem>
                <ElFormItem label="测试项3">
                  <ElInput v-model={data.test3}></ElInput>
                </ElFormItem>
              </ElCollapseItem>
              <ElCollapseItem title="检验/验证特性" name="2"></ElCollapseItem>
              <ElCollapseItem title="实验要求" name="3"></ElCollapseItem>
              <ElCollapseItem title="限制要求" name="4"></ElCollapseItem>
              <ElCollapseItem title="ASR检验信息" name="5"></ElCollapseItem>
              <ElCollapseItem title="限寿要求" name="6"></ElCollapseItem>
            </ElCollapse>
          )
        }
      }
    }
  }
])

const rules = reactive({
  qar_no: [lengthRange({ max: 20 })],
  part_no: [lengthRange({ max: 40 })],
  part_type: [lengthRange({ max: 20 })],
  part_name: [lengthRange({ max: 40 })],
  product_type: [lengthRange({ max: 20 })],
  form_type: [lengthRange({ max: 100 })],
  purchase_order: [lengthRange({ max: 100 })],
  flight: [lengthRange({ max: 30 })],
  quantity_weight: [lengthRange({ max: 20 })],
  vendor: [lengthRange({ max: 40 })],
  rev_letter: [lengthRange({ max: 20 })],
  temper: [lengthRange({ max: 20 })],
  zpm_cpm: [lengthRange({ max: 20 })],
  typing: [lengthRange({ max: 20 })],
  classing: [lengthRange({ max: 20 })],
  grade: [lengthRange({ max: 20 })],
  form: [lengthRange({ max: 20 })],
  size: [lengthRange({ max: 20 })],
  color: [lengthRange({ max: 20 })],
  material_type: [lengthRange({ max: 20 })],
  spec_stan: [lengthRange({ max: 20 })],
  spec_stan_rev: [lengthRange({ max: 20 })],
  mat_code: [lengthRange({ max: 20 })],
  test_items: [lengthRange({ max: 20 })],
  required_dimension: [lengthRange({ max: 20 })],
  pclr_quantity: [lengthRange({ max: 20 })],
  pclr_size: [lengthRange({ max: 20 })],
  plan_remark: [lengthRange({ max: 100 })],
  life_limit_req: [lengthRange({ max: 20 })],
  life_limit_period: [lengthRange({ max: 20 })],
  life_limit_manage: [lengthRange({ max: 20 })],
  special_property: [lengthRange({ max: 20 })],
  manufacturer: [lengthRange({ max: 40 })],
  change_no: [lengthRange({ max: 20 })],
  draw_no: [lengthRange({ max: 20 })],
  draw_no_rev: [lengthRange({ max: 20 })],
  software_code: [lengthRange({ max: 20 })],
  software_rev: [lengthRange({ max: 20 })],
  atp_no: [lengthRange({ max: 20 })],
  atp_rev: [lengthRange({ max: 20 })],
  insp_remark: [lengthRange({ max: 100 })],
  act_measurement: [lengthRange({ max: 20 })],
  product_information: [lengthRange({ max: 40 })],
  product_no: [lengthRange({ max: 40 })],
  product_name: [lengthRange({ max: 40 })],
  product_msn: [lengthRange({ max: 40 })],
  remark: [lengthRange({ max: 100 })],
  serial_no: [lengthRange({ max: 200 })],
  lot_batch_no: [lengthRange({ max: 200 })],
  act_part_rev: [lengthRange({ max: 20 })],
  act_change_no: [lengthRange({ max: 20 })],
  act_draw_no: [lengthRange({ max: 20 })],
  act_draw_no_rev: [lengthRange({ max: 20 })],
  act_software_code: [lengthRange({ max: 20 })],
  act_software_rev: [lengthRange({ max: 20 })],
  act_atp_no: [lengthRange({ max: 20 })],
  act_atp_rev: [lengthRange({ max: 20 })],
  actual_weight: [lengthRange({ max: 20 })],
  source_of_weight: [lengthRange({ max: 20 })],
  acceptable_quant: [lengthRange({ max: 20 })],
  rejective_quant: [lengthRange({ max: 20 })],
  conformity_rate: [lengthRange({ max: 20 })],
  ok_to_file: [lengthRange({ max: 10 })]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    return await getFormData()
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" :is-col="true" />
</template>

<style lang="less"></style>
